/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import com.sun.lwuit.Image;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Mobile
 */
public class RMSAccess {

    RecordStore rStore;
    String rmsName = "popular_image_rms";

    public RMSAccess() {
    }

    public RMSAccess(String rmsName) {
        this.rmsName = rmsName;
    }

    public Image load(int index) {
        Image image = null;
        try {
            image = Image.createImage(rStore.getRecord(index), 0, rStore.getRecord(index).length);
            System.out.println("Load Image");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            try {
                if (index == 1)
                    image = Image.createImage("/images/1.jpg");
                else if(index == 2)
                    image = Image.createImage("/images/2.jpg");
                else if (index == 3)
                    image = Image.createImage("/images/3.jpg");
                else if (index == 4)
                    image = Image.createImage("/images/4.jpg");
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }
        return image;
    }

    public boolean isEmpty() {

        try {
            System.out.println("Num record : " + rStore.getNumRecords());
            if (rStore.getNumRecords() < 1) {
                return true;
            } else {
                return false;
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return true;
        }
    }

    public void open() {
        try {
            rStore = RecordStore.openRecordStore(rmsName, true);
            System.out.println("Open RMS");
            System.out.println("Size " + rStore.getSize());
            System.out.println("Size avalilabe " + rStore.getSizeAvailable());
        } catch (RecordStoreException recordstoreexception) {
            recordstoreexception.printStackTrace();
        }
    }

    public void removeAll(){
        try {
            rStore.closeRecordStore();
            rStore.deleteRecordStore(rmsName);
            open();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public void close() {
        try {
            rStore.closeRecordStore();
            System.out.println("Close RMS");
        } catch (RecordStoreNotOpenException recordstorenotopenexception) {
            recordstorenotopenexception.printStackTrace();
        } catch (RecordStoreException recordstoreexception) {
            recordstoreexception.printStackTrace();
        }
    }

    public boolean saveByteToRMS(byte[] bytes) throws IOException {
        if (bytes != null) {
            try {
                rStore.addRecord(bytes, 0, bytes.length);
                System.out.println("Data Byte to RMS Saved");
                System.out.println("Size " + rStore.getSize());
                System.out.println("Size avalilabe " + rStore.getSizeAvailable());
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
                return false;
            }
        }

        return true;
    }

    private static byte[] convertIntToByteArray(int integer) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (integer >> 24);
        bytes[1] = (byte) ((integer << 8) >> 24);
        bytes[2] = (byte) ((integer << 16) >> 24);
        bytes[3] = (byte) ((integer << 24) >> 24);
        return bytes;
    }

    private static byte[] convertIntArrayToByteArray(int[] integers) {
        byte[] bytes = new byte[integers.length * 4];
        for (int index = 0; index < integers.length; index++) {
            byte[] integerBytes = convertIntToByteArray(integers[index]);
            bytes[index * 4] = integerBytes[0];
            bytes[1 + (index * 4)] = integerBytes[1];
            bytes[2 + (index * 4)] = integerBytes[2];
            bytes[3 + (index * 4)] = integerBytes[3];
        }
        return bytes;
    }

    public boolean saveImageToRMS(Image img) {
        byte[] imgRgbData = convertIntArrayToByteArray(img.getRGBCached());
        try {

            rStore.addRecord(imgRgbData, 0, imgRgbData.length);  // Add record
            System.out.println("Image Save to RMS");
            System.out.println("Size " + rStore.getSize());
            System.out.println("Size avalilabe " + rStore.getSizeAvailable());
        } catch (RecordStoreNotFoundException rsnfe) {
            // Record storage not foundbaos
            rsnfe.printStackTrace();
            return false;
        } catch (RecordStoreException rse) {
            // Other record storage problem
            System.out.println("Image terlalu gede");
            rse.printStackTrace();
            return false;
        }

        return true; // We've successfuly done
    }

    public void writeRecord(String str) {
        byte[] rec = str.getBytes();

        try {
            rStore.addRecord(rec, 0, rec.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int readRecords(int index) {
        int result = 0;
        try {
            byte[] recData = new byte[10];
            int len;
            // Re-allocate if necessary
            if (rStore.getRecordSize(index) > recData.length) {
                recData = new byte[rStore.getRecordSize(index)];
            }
            len = rStore.getRecord(index, recData, 0);
            result = Integer.parseInt(new String(recData, 0, len));
            System.out.println("Record #" + index + ": " + result);

        } catch (Exception e) {
            
            System.out.print("Defaul value "+ result);
        }
        return result;
    }
}
